package com.zzh.lib.core.utils;

import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.zzh.lib.core.HLibrary;
import com.zzh.lib.core.R;

import java.lang.ref.WeakReference;


/**
 * Created by Administrator.
 *
 * @date: 2019/8/15
 * @email: zzh_hz@126.com
 * @QQ: 1299234582
 * @author: zzh
 * @description: 提示消息
 * @since 1.0
 */
public class ToastUtils {

    public static final Handler sHandler = new Handler(Looper.getMainLooper());

    private static Toast sToast;

    private static boolean showVae = false;
    private static int layoutResId = 0;

    private static int gravity = Gravity.CENTER;
    private static int xOffset = 0;
    private static int yOffset = 0;

    /**
     * 设置吐司位置
     *
     * @param gravity 位置
     * @param xOffset x偏移
     * @param yOffset y偏移
     */
    public static void setGravity(int gravity, int xOffset, int yOffset) {
        ToastUtils.gravity = gravity;
        ToastUtils.xOffset = xOffset;
        ToastUtils.yOffset = yOffset;
    }

    private static WeakReference<View> sViewWeakReference;

    private ToastUtils() {
    }

    public static void setShowVae(boolean showVae) {
        ToastUtils.showVae = showVae;
    }

    public static void setToastView(int layoutResId) {
        ToastUtils.layoutResId = layoutResId;
    }

    public static void update() {
        mVaeView = null;
        sViewWeakReference = null;
    }

    /**
     * 短提示
     *
     * @param text 提示文字
     */
    public static void show(CharSequence text) {
        show(text, Toast.LENGTH_SHORT);
    }


    /**
     * @param msg      提示的文字
     * @param duration 提示长短
     */
    public static void show(CharSequence msg, int duration) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            if (showVae) {
                showInternalView(msg, duration);
            } else {
                showInternal(msg, duration);
            }

        } else {
            sHandler.post(() -> {
                if (showVae) {
                    showInternalView(msg, duration);
                } else {
                    showInternal(msg, duration);
                }
            });
        }
    }

    /**
     * 提示信息
     *
     * @param text     提示文字
     * @param duration 长短提示
     *                 提示文字显示在屏幕中心
     */
    private static void showInternal(CharSequence text, int duration) {
        if (TextUtils.isEmpty(text)) {
            return;
        }
        if (sToast != null) {
            sToast.setText(text);
            sToast.setDuration(duration);
        } else {
            sToast = Toast.makeText(HLibrary.getInstance().getContext(), text, duration);
        }
        sToast.setGravity(Gravity.CENTER, 0, 0);
        sToast.show();
    }

    static View mVaeView;

    private static void showInternalView(CharSequence text, int duration) {
        if (TextUtils.isEmpty(text)) {
            return;
        }
        if (layoutResId == 0) {
            showInternal(text, duration);
            return;
        }

        if (sToast == null) {
            sToast = new Toast(HLibrary.getAppContext());
        }
        if (sViewWeakReference != null && sViewWeakReference.get() != null) {
            sToast.setView(sViewWeakReference.get());
        } else {
            mVaeView = LayoutInflater.from(HLibrary.getAppContext()).inflate(layoutResId, null);
            sViewWeakReference = new WeakReference<>(mVaeView);
            sToast.setView(mVaeView);
        }

        TextView textView = mVaeView.findViewById(R.id.h_lib_toast_message);
        textView.setText(text);
        sToast.setGravity(gravity, xOffset, yOffset);
        sToast.setDuration(duration);
        sToast.show();
    }

}
